package com.example.cloud.service.business.medical.impl;

import com.example.cloud.dao.business.medical.SkinType;
import com.example.cloud.dao.business.medical.SkinTypeRepository;
import com.example.cloud.dao.business.reservation.PatientRepository;
import com.example.cloud.model.business.reservation.Patient;
import com.example.cloud.service.business.medical.SkinTypeService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.Date;

/**
 * Created by wangtao on 2018/6/6.
 */
@Service
public class SkinTypeServiceImpl implements SkinTypeService {

    private static final Logger logger = Logger.getLogger(SkinTypeServiceImpl.class);

    @PersistenceContext
    EntityManager em;

    @Autowired
    private SkinTypeRepository skinTypeRepository;

    @Autowired
    private PatientRepository patientRepository;

    @Override
    @Transactional
    public void doSave(SkinType record) {
        if(record.getId() == null){
            record.setLockFlag(0);
            Patient p = patientRepository.findOne(record.getPatientId());
            if(p != null){
                record.setAccountId(p.getAccountId());
            }
            record.setCreationTime(new Date());
        }
        skinTypeRepository.save(record);
    }
}
